﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using Medianamik.Core.Synchronization;
using Medianamik.Globalization;
using Telerik.Web.UI;

namespace Medianamik.UI.Controls
{
    internal class SubGridActionColumnTemplate : ITemplate
    {
        private ImageButton _editButton;
        private ImageButton _deleteButton;
        private readonly CommandEventHandler _editSubItemHandler;
        private readonly CommandEventHandler _deleteSubItemHandler;

        public SubGridActionColumnTemplate(CommandEventHandler editSubItemHandler,
                                           CommandEventHandler deleteSubItemHandler)
        {
            _editSubItemHandler = editSubItemHandler;
            _deleteSubItemHandler = deleteSubItemHandler;
        }

        private void BindDeleteButton(Object s, EventArgs e)
        {
            var container = (GridDataItem)_deleteButton.NamingContainer;
            var node = ((Record)container.DataItem);
            _deleteButton.CommandArgument = node.Id.ToString();
        }

        private void BindEditButton(Object s, EventArgs e)
        {
            var container = (GridDataItem)_editButton.NamingContainer;
            var node = ((Record)container.DataItem);

            if (!node.ExistsInCurrentCulture)
            {
                container.CssClass = "rgRow ElementNonTraduit";
                _editButton.ImageUrl = "/Medianamik/images/Icones/Ico_Plus.gif";
            }

            _editButton.CommandArgument = node.Id.ToString();
        }

        public void InstantiateIn(Control container)
        {
            var table = new Table
                            {
                                CellPadding = 0,
                                CellSpacing = 0,
                                BorderWidth = 0
                            };

            table.Attributes.Add("align", "center");
            container.Controls.Add(table);

            var tableRow = new TableRow();
            table.Rows.Add(tableRow);

            var editTableCell = new TableCell();
            tableRow.Cells.Add(editTableCell);

            _editButton = new ImageButton
                              {
                                  ID = "editButton",
                                  ImageUrl = "/Medianamik/images/Icones/Ico_Edition.gif"
                              };

            _editButton.Command += _editSubItemHandler;
            _editButton.DataBinding += BindEditButton;
            _editButton.Attributes.Add("title", Translator
                                                    .Translate("Medianamik_global", "Action_EditTooltip"));
            editTableCell.Controls.Add(_editButton);

            var deleteTableCell = new TableCell();
            tableRow.Cells.Add(deleteTableCell);

            _deleteButton = new ImageButton
                                {
                                    ID = "deleteButton",
                                    ImageUrl = "/medianamik/images/Icones/Ico_Supprimer.gif"
                                };

            _deleteButton.Command += _deleteSubItemHandler;
            _deleteButton.DataBinding += BindDeleteButton;
            _deleteButton.Attributes.Add("title",
                                         Translator.Translate("Medianamik_global", "Action_DeleteTooltip"));
            deleteTableCell.Controls.Add(_deleteButton);
        }
    }
}